import { _decorator, Component, director, find, Label, Node, ProgressBar, resources, Sprite, SpriteFrame } from 'cc';
import { headComp } from '../mainui/headComp';
import { GameEvent } from '../../GameEvent';
import { lobby } from '../../lobby';
import { fishInfoComp } from './fishInfoComp';
import { Utils } from '../../utils/Utils'
import protocol from 'db://assets/proto/protoExt';

const { ccclass, property } = _decorator;





@ccclass('fishComp')
export class fishComp extends Component {


    @property(ProgressBar)
    progressbar!: ProgressBar;

    @property(Label)
    statusLabel!: Label;

    @property(headComp)
    head!: headComp;

    id: string = "";
    start() {
        director.on(GameEvent.updateFish, this.updateFish, this)
        director.on(GameEvent.removeFish, this.removeFish, this)
        this.node.on(Node.EventType.TOUCH_END, () => {
            let fishInfoUI = lobby.instance.addUI("prefab/fishInfo")
            fishInfoUI.getComponent(fishInfoComp)!.UpdateUI(this.id)
        })
    }
    updateFish(fish: protocol.new_world.shared.v1.IImmortalDetail) {
        this.setFish(fish.fish)
    }
    removeFish(fish: protocol.new_world.shared.v1.IImmortalDetail) {
        if (this.id == fish.fish.id) {
            this.node.destroy()
        }
    }
    setFish(fish: protocol.new_world.shared.v1.IFreshImmortal) {
        this.id = fish.id!;

        this.head.setHead(fish.id!)

        let curhp = fish.battle_info!.heal! as number
        let maxhp = fish.battle_info!.max_heal! as number
        this.progressbar.progress = curhp / maxhp;

        this.statusLabel.string = Utils.getStatusStr(fish.status!)
    }
    protected onDestroy(): void {
        director.off(GameEvent.updateFish, this.updateFish, this)
        director.off(GameEvent.removeFish, this.removeFish, this)
    }
    update(deltaTime: number) {

    }
}


